<?php
class PaymentSmsWapmasterController extends CpController
{
    public function actionIndex()
    {
        /* Lấy tất cả đầu số trong dữ liệu */
        $serviceNumber = ASmsWapmaster::getAllserviceNumber();
        
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 30;
        
        $search = array();
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "01-" . date("m-Y");        
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : date("d-m-Y");        
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";
        $search["telco"] = isset($_GET["telco"]) ? trim(strip_tags($_GET["telco"])) : "";
        $search["serviceNumber"] = isset($_GET["serviceNumber"]) ? trim(strip_tags($_GET["serviceNumber"])) : "";
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["order"] = isset($_GET["order"]) ? trim(strip_tags($_GET["order"])) : "";        
        
        $sms = array();
        $totalUser = 0;        
        $numberPage = 0;        
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){
            
            $toDate = date("Y-m", strtotime($search["toDate"])) . "-01";
            $fromDate = date("Y-m", strtotime($search["fromDate"])) . "-01";            
            /*
            * Lấy tỷ lệ ăn chia
            */   
            
            $rate_sms = ARate::getAllRateSms();
            
            /* Lấy danh sách user phát sinh doanh thu trong khoảng thời gian tìm kiếm */
            $users = ASmsWapmaster::GetUser($search, $currentPage, $numberRecordPerPage);
            $totalUser = ASmsWapmaster::GetTotalUser($search);
            $numberPage = ceil($totalUser / $numberRecordPerPage);
            
            if(!empty($users)){
                $search["list_user_id"] = "";
                foreach($users as $row){
                    $search["list_user_id"] .= ", " . $row["userId"];
                }        
                $search["list_user_id"] = ltrim($search["list_user_id"], ", ");
                
                /* Lấy log sms của các thành viên này */
                $sms = ASmsWapmaster::getReport($search, $rate_sms);                
            }   
        }        
        
        $this->render(
            "index"
            , array(
                "search"=>$search
                , "serviceNumber"=>$serviceNumber
                , "sms"=>$sms
                , "totalUser"=>$totalUser
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage
            )
        );    
    }
    
    public function actionDetail()
    {
        /* Lấy tất cả đầu số trong dữ liệu */
        $serviceNumber = ASmsWapmaster::getAllserviceNumber();
        
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 50;
        
        $search = array();
        $search["userId"] = isset($_GET["userId"]) ? intval($_GET["userId"]) : 0;
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "";
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : "";        
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";
        $search["telco"] = isset($_GET["telco"]) ? trim(strip_tags($_GET["telco"])) : "";
        $search["serviceNumber"] = isset($_GET["serviceNumber"]) ? trim(strip_tags($_GET["serviceNumber"])) : "";
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["type"] = isset($_GET["type"]) ? trim(strip_tags($_GET["type"])) : "";
        
        $sms = array();
        $totalSms = 0;        
        $numberPage = 0;
        
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){
            $toDate = date("Y-m", strtotime($search["toDate"])) . "-01";
            $fromDate = date("Y-m", strtotime($search["fromDate"])) . "-01";
           
            /*
            * Lấy tỷ lệ ăn chia
            */   
            
            $rate_sms = ARate::getAllRateSms();
            
            /* Lấy log sms của các thành viên này */
            $sms = ASmsWapmaster::getReportByUser($search, $rate_sms, $currentPage, $numberRecordPerPage);            
            $totalSms = ASmsWapmaster::getTotalReportByUser($search);            
            $numberPage = ceil($totalSms / $numberRecordPerPage);
        }

        $this->render(
            "detail"
            , array(
                "search"=>$search
                , "serviceNumber"=>$serviceNumber
                , "sms"=>$sms
                , "totalSms"=>$totalSms
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage
            )
        );   
    }
    
    
}  
?>
